package leetcode.Y2024M5D23;

import org.junit.Test;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

public class S219 {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        // 滑动窗口解决
        HashSet<Integer> set = new HashSet<>();
        for(int i = 0; i < nums.length; i++) {
            if(set.contains(nums[i])) {
                return true;
            }
            set.add(nums[i]);
            if(set.size() > k) {
                set.remove(nums[i - k]);
            }
        }
        return false;


    }

    @Test
    public void test() {
        int[] nums = {1, 2, 3, 1};
        int k = 3;
        int[] nums1 = {1, 0, 1, 1};
        int k1 = 1;
        int[] nums2 = {1, 2, 3, 1, 2, 3};
        int k2 = 2;
        System.out.println(containsNearbyDuplicate(nums, k));
        System.out.println(containsNearbyDuplicate(nums1, k1));
        System.out.println(containsNearbyDuplicate(nums2, k2));
    }
}
